package com.qidian.mespro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mespro.mapper.WorkflowProductMapper;
import com.qidian.mespro.service.WorkflowProductService;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.WorkflowProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ author: 周晓轩
 * @ company 起点编程
 * @ time: 2025/9/19 - 18:26
 * @ Description:
 */
@Service
public class WorkflowProductServiceImpl implements WorkflowProductService {

    @Autowired
    private WorkflowProductMapper workflowProductMapper;

    @Override
    public Result<PageInfo<WorkflowProduct>> selectByWorkflowId(Long workflowId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<WorkflowProduct> workflowProducts = workflowProductMapper.selectByWorkflowId(workflowId);
        return Result.success(new PageInfo<>(workflowProducts));
    }

    @Override
    public Result<WorkflowProduct> addWorkflowProduct(WorkflowProduct workflowProduct) {
        if(workflowProduct.getProductCode()==null){
            return Result.error("产品编码不能为空");
        }
        if(workflowProduct.getWorkflowId()==null){
            return Result.error("工艺流程id不能为空");
        }

        int count = workflowProductMapper.countByWorkflowIdAndProductCode(workflowProduct.getWorkflowId(), workflowProduct.getProductCode().trim());
        if (count > 0) {
            return Result.error("当前工艺流程下已存在产品编码为[" + workflowProduct.getProductCode() + "]的产品，请更换编码");
        }

        return workflowProductMapper.addWorkflowProduct(workflowProduct)== 1?Result.success(workflowProduct):Result.error("保存失败");
    }

    @Override
    public Result<WorkflowProduct> selectWorkflowProductById(Long id) {
        return Result.success(workflowProductMapper.selectWorkflowProductById(id));
    }

    @Override
    public Result updateWorkflowProduct(WorkflowProduct workflowProduct) {
        if (workflowProduct.getId() == null) {
            return Result.error("ID不能为空");
        }
        if (workflowProduct.getProductCode() == null || workflowProduct.getProductCode().trim().isEmpty()) {
            return Result.error("产品编码不能为空");
        }
        if (workflowProduct.getWorkflowId() == null) {
            return Result.error("工艺流程id不能为空");
        }

        int count = workflowProductMapper.countByWorkflowIdAndProductCodeExcludeId(
                workflowProduct.getWorkflowId(),
                workflowProduct.getProductCode().trim(),
                workflowProduct.getId()
        );
        if (count > 0) {
            return Result.error("当前工艺流程下已存在产品编码为[" + workflowProduct.getProductCode() + "]的产品，请更换编码");
        }
        return workflowProductMapper.updateWorkflowProduct(workflowProduct)== 1 ? Result.success():Result.error("编辑失败");
    }

    @Override
    public Result deleteWorkflowProductById(Long id) {
        return workflowProductMapper.deleteWorkflowProductById(id) == 1?Result.success():Result.error("删除失败");
    }

    @Override
    public Result batchDelete(Long[] ids) {
        return workflowProductMapper.batchDelete(ids) > 0?Result.success():Result.error("删除失败");
    }
}
